package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.excel.Account_lookExcel;
import com.huike.excel.HospitalManagementExcel;
import com.huike.excel.InpatientSettlementExcel;
import com.huike.pojo.Charge;
import com.huike.pojo.Drug;
import com.huike.pojo.Hospital;
import com.huike.pojo.Register;
import com.huike.service.CheckChargeService;
import com.huike.service.DrugService;
import com.huike.service.HospitalService;
import com.huike.service.RegisterService;
import com.huike.vo.ExportExcel;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping(value = "InpatientSettlementController")
public class InpatientSettlementController {
    @Autowired
    private RegisterService registerService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private CheckChargeService checkChargeService;
    @Autowired
    private DrugService drugService;
//住院结算分页查询
    @RequestMapping(value = "PaginationSearch")
    public ResultVO<PageInfo> PaginationSearch(Integer id, String registerName,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Register> registers = registerService.PaginationSearchForISC(id, registerName);
        PageInfo pageInfo = new PageInfo(registers, 5);
        return new ResultVO<PageInfo>(pageInfo);
    }
//结算具体项目的分页查询
    @RequestMapping(value = "PaginationSearchForEnd")
    public ResultVO<PageInfo> PaginationSearchForEnd(Integer registerId,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        Hospital hospital = hospitalService.getHospitalById(registerId);
        Integer id = hospital.getId();
        List<Charge> charges = checkChargeService.infoRefisterCharge(id);
        PageHelper.startPage(pageNum, pageSize);
        List<Drug> drugs = drugService.getDrugsAndChargeByHospitalId(id);
        for (int i = 0; i < charges.size(); i++) {
            Charge charge = charges.get(i);
            String cname = charge.getChargeName();
            Double cmoney = charge.getChargeMoney();
            Date date = charge.getChargeDate();
            drugs.add(new Drug(cmoney, cname, date));
        }//两个list合并
        PageInfo pageInfo = new PageInfo(drugs, 5);
        return new ResultVO<PageInfo>(pageInfo);
    }
    //结算具体项目的分页查询
    @RequestMapping(value = "PaginationSearchForLook")
    public ResultVO<PageInfo> PaginationSearchForLook(Integer registerId,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        Hospital hospital = hospitalService.getHospitalById(registerId);
        Integer id = hospital.getId();
        PageHelper.startPage(pageNum, pageSize);
        List<Drug> drugs = drugService.getDrugsAndChargeByHospitalId(id);
        PageInfo pageInfo = new PageInfo(drugs, 5);
        return new ResultVO<PageInfo>(pageInfo);
    }
//导出住院结算首页的Excel
    @RequestMapping("excelInfo")
    public ResultVO excelInfo(String outStream, Integer id, String registerName) {
        List<Register> list = registerService.PaginationSearchForISC(id, registerName);
        List<InpatientSettlementExcel> inpatientSettlementExcels = new ArrayList<>();//存储要生成文件的字段
        for (int i = 0; i < list.size(); i++) {
            InpatientSettlementExcel inpatientSettlementExcel1 = new InpatientSettlementExcel();
            Register register = list.get(i);
            inpatientSettlementExcel1.setId(register.getId());
            inpatientSettlementExcel1.setRegisterName(register.getRegisterName());
            inpatientSettlementExcel1.setDeposit(register.getHospital().getDeposit());
            inpatientSettlementExcel1.setRestCost(register.getHospital().getRestCost());
            inpatientSettlementExcel1.setEndStatus(register.getHospital().getEndStatus() == 0 ? "未结算" : "已结算");
            inpatientSettlementExcels.add(inpatientSettlementExcel1);
        }

        String[] headerStr = {"病历号", "姓名", "押金", "余额", "状态"};//表头
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = outStream + "\\" + sdf.format(new Date()) + "住院结算表.xlsx";//生成文件名称
        ExportExcel exportExcel = new ExportExcel();
        try {
            OutputStream outputStream = new FileOutputStream(fileName);
            exportExcel.exportExcel(inpatientSettlementExcels, outputStream, headerStr);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO<>(500, "导出时未知异常");
        }
        return new ResultVO<>(200, "导出成功");
    }
//导出详情界面的Excel
    @RequestMapping("excelInfoForLook")
    public ResultVO excelInfoForLook(String outStream, Integer registerId, String registerName) {
        Hospital hospital = hospitalService.getHospitalById(registerId);
        Integer id = hospital.getId();
        List<Charge> charges = checkChargeService.infoRefisterCharge(id);
        List<Drug> drugs = drugService.getDrugsByHospitalId(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Account_lookExcel> account_lookExcels = new ArrayList<>();//存储想要生成文件的字段
        for (int i = 0; i < charges.size(); i++) {
            Account_lookExcel account_lookExcel1 = new Account_lookExcel();
            Charge charge = charges.get(i);
            account_lookExcel1.setId(id);
            account_lookExcel1.setRegisterName(registerName);
            account_lookExcel1.setItemName(charge.getChargeName());
            account_lookExcel1.setChargeMoney(charge.getChargeMoney());
            account_lookExcel1.setDate(sdf.format(charge.getChargeDate()));
            account_lookExcels.add(account_lookExcel1);
        }
        for (int j = 0; j < drugs.size(); j++) {
            Account_lookExcel account_lookExcel1 = new Account_lookExcel();
            Drug drug = drugs.get(j);
            account_lookExcel1.setId(id);
            account_lookExcel1.setRegisterName(registerName);
            account_lookExcel1.setItemName(drug.getDrugName());
            account_lookExcel1.setChargeMoney(drug.getOutPrice());
            account_lookExcel1.setDate(sdf.format(drug.getChargeDate()));
            account_lookExcels.add(account_lookExcel1);
        }
        System.out.println(account_lookExcels);
        String[] headerStr = {"病历号", "姓名", "收费项目", "收费金额", "收费日期"};//表头
        String fileName = outStream + "\\" + sdf.format(new Date()) + "项目详细表.xlsx";//生成文件名称
        ExportExcel exportExcel = new ExportExcel();
        try {
            OutputStream outputStream = new FileOutputStream(fileName);
            exportExcel.exportExcel(account_lookExcels, outputStream, headerStr);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO<>(500, "导出时未知异常");
        }
        return new ResultVO<>(200, "导出成功");
    }
//住院结算
    @RequestMapping(value = "Settlement",method = RequestMethod.POST)
    public ResultVO Settlement(Integer registerId, Double total, Double restCost) {
        Hospital hospital = hospitalService.getHospitalById(registerId);
        Integer id = hospital.getId();
        List<Charge> charges = checkChargeService.infoRefisterCharge(id);
        List<Drug> drugs = drugService.getDrugsByHospitalId(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String ChargeDate = sdf.format(new Date());
        for (int i = 0; i < charges.size(); i++) {
            Charge charge = charges.get(i);
            if(charge.getChargeDate()==null){
                checkChargeService.updateChargeDate(charge.getId(),ChargeDate);
            }
        }
        for (int j = 0; j < drugs.size(); j++) {
            Drug drug = drugs.get(j);
            if(drug.getChargeDate()==null){
                drugService.updateChargeDate(drug.getId(),ChargeDate);
            }
        }
        Double Total =  hospital.getTotalCost() + total;
        hospitalService.Settlement(registerId,Total,restCost);
        return new ResultVO<>();
    }
}
